import cv2
import os
import numpy as np
img = cv2.imread(r'C:\Users\Admin\Desktop\model\pytorch-deeplab-xception-master\deep_info\img3.png',-1)
path = r'C:\Users\Admin\Desktop\model\pytorch-deeplab-xception-master\deep_info\subpictures\\'
# img = cv2.imread(path+'result.png', -1)
goal = 9440
img = np.pad(img, (((goal-img.shape[0])//2, goal-img.shape[0]-(goal-img.shape[0])//2), ((goal-img.shape[0])//2, goal-img.shape[1]-(goal-img.shape[0])//2)))
def pic_split(img, path_out):
    imgs = list(np.vsplit(img, 20))
    out = []
    for v_pic in imgs:
        for pic in list(np.hsplit(v_pic,20)):
            out.append(pic)
    for i in range(len(out)):
        cv2.imwrite(path_out+f'{i+1}.png', out[i])
def mapping(img, path_out):
    maximum = np.max(img)
    img_copy = img.copy()
    if maximum:
        minimum = np.min(img[img > 0])
        bins = np.linspace(minimum,maximum,65536)
        for i in range(65535):
            if i % 100 == 0:
                print(i)
            img_copy[(img>=bins[i])*(img<bins[i+1])] = i+1
        img_copy[img<0] = 0
        img_copy[img==maximum] = 65535
    cv2.imwrite(path_out, img_copy)
print('splitting...')
pic_split(img, path)
print('split done')
for p in os.listdir(path):
    print(p)
    cur_img = cv2.imread(path+p, -1)
    mapping(cur_img, path+'processed\\'+p)
# cv2.imshow('img3_p', img)

# cv2.waitKey(0)

# img = np.concatenate((np.concatenate((np.zeros((1,img.shape[1])),img), axis=0), np.zeros((img.shape[0]+1,1))), axis=1)
# img1, img2 = np.hsplit(img, 2)
# imgs = [0,0,0,0]
# imgs[0], imgs[2] = np.vsplit(img1,2)
# imgs[1], imgs[3] = np.vsplit(img2,2)
# for i in range(4):
#     print(i)
#     cv2.imwrite(path+f'img{i+1}.png', imgs[i])
